<?php

namespace Common\Model;

/**
 * 父订单信息
 * @modify trainyao 2016-01-05 创建
 * @index hotelchannel_id(`hotelchannel_id`)
 */
Class OrderInfoModel extends BaseModel {

    protected $trueTableName = 'tb_order_info';

    protected function _before_insert(&$data, $options) {
        $data['creator'] = getUserId();
    }

    protected function _before_update(&$data, $options) {
        // parent::_before_update($data, $options);
    }

    /**
     * 获取父订单信息
     * @modify trainyao 2016-01-05 创建
     * @param  mixin   $orderId    订单id(s)
     * @param  mixin   $brief    是否获取概要信息,默认是
     * @return array               订单信息数组
     */
    public function getInfo($orderId, $brief = true) {
        $option = array();
        if (is_array($orderId)) {
            $orderId = implode(',', $orderId);
        }
        $field = 'id,hotelentity_id,hotelchannel_id,payment,mobile,name,areacode,charityprice,status';
        if ($brief !== true) {
            $field = 'id,hotelentity_id,hotelchannel_id,payment,orig,mobile,name,deposit,receipt,userlogin_id,identity,discount,discountrule,remark,areacode,charityprice,status';
        }

        if (strpos($orderId, ',') !== false) {
            $option['id'] = array('in', $orderId);
            $data = $this->where($option)->field($field)->select();
        } else {
            $option['id'] = $orderId;
            $data = $this->where($option)->field($field)->find();
        }

        return empty($data) ? array() : $data;
    }

    /**
     * 根据客栈查询订单
     * @modify trainyao 2016-01-05 创建
     * @param  integer   $hotelId   客栈id
     * @param  mixin   $brief       是否获取概要信息,默认是
     * @return array                订单信息,
     */
    public function getInfoByHotel($hotelId, $brief = true) {
        $option = array(
            'hotelentity_id' => $hotelId
        );
        $field = 'id,hotelentity_id,hotelchannel_id,payment,mobile,name,areacode,status';
        if ($brief !== true) {
            $field = 'id,hotelentity_id,hotelchannel_id,deposit,payment,receipt,userlogin_id,mobile,name,identity,remark,areacode,status';
        }

        $data = $this->where($option)->field($field)->limit(20)->select();
        return empty($data) ? array() : $data;
    }

    /**
     * 根据预定人获取订单
     * @modify trainyao 2016-05-10   创建
     * @param  mixin   $userLoginId   预定人ID
     * @return array                信息数组
     */
    public function getInfoByUser($userLoginId) {
        $option = array();
        if (is_array($userLoginId)) {
            $userLoginId = implode(',', $userLoginId);
        }
        if (strpos($userLoginId, ',') === false) {
            $option['userlogin_id'] = $userLoginId;
        } else {
            $option['userlogin_id'] = array('in', $userLoginId);
        }
        $data = $this->where($option)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 根据客栈查询预定人姓名
     * @modify zhangzhen 2017-02-15 创建
     * @param  integer   $hotelId   客栈id
     * @param  mixin   $mobile      手机号码
     * @return array                订单信息,
     */
    public function getNameByMobile($hotelId, $mobile, $areaCode = null) {
        $option = array(
            'hotelentity_id' => $hotelId,
            'mobile' => $mobile,
        );
        if (!is_null($areaCode)) {
            $option['areacode'] = $areaCode;
        }
        $data = $this->where($option)->field('name')->order('id desc')->find();
        return empty($data) ? array() : $data;
    }

    /**
     * 根据手机号码查询预定人信息
     * @modify zhangzhen 2017-02-15 创建
     * @param  integer   $hotelId   客栈id
     * @param  mixin   $mobile      手机号码
     * @return array                订单信息,
     */
    public function getInfosByMobile($hotelId, $mobile, $areaCode = null) {
        $option = array(
            'info.hotelentity_id' => $hotelId,
            'info.mobile' => $mobile,
            'info.status' => 3,
        );
        if (!is_null($areaCode)) {
            $option['info.areacode'] = $areaCode;
        }
        $data = $this->alias('info')
                    ->field('info.id,info.hotelentity_id,channel.name,info.payment,info.orig,info.createtime')
                    ->join('tb_hotel_channel as channel ON channel.id = info.hotelchannel_id','LEFT')
                    ->where($option)
                    ->order('info.id desc')
                    ->select();
        return empty($data) ? array() : $data;
    }


    /**
     * 根据客栈查询预定人证件号
     * @modify zhangzhen 2017-02-15 创建
     * @param  integer   $hotelId   客栈id
     * @param  mixin   $mobile      手机号码
     * @return array                订单信息,
     */
    public function getIdentityByMobile($hotelId, $mobile, $areaCode = null) {
        $option = array(
            'hotelentity_id' => $hotelId,
            'mobile' => $mobile,
            'identity' => array('neq',''),
        );
        if (!is_null($areaCode)) {
            $option['areacode'] = $areaCode;
        }
        $data = $this->where($option)->field('identity')->order('id desc')->find();
        return empty($data) ? array() : $data;
    }
    /**
     * 根据客栈Id获取客人信息
     * @modify zhangzhen 2017-02-14   创建
     * @param  integer   $hotelId     酒店ID(多选)
     * @param  mobile    $mobile      手机号
     * @param  integer   $offset      开始条数
     * @param  integer   $endOffset   页条数
     * @return array                信息数组
     */
    public function getCustomerByHotel($hotelId, $mobile, $offset, $pageSize, $areaCode = null) {
        $option = array(
            'hotelentity_id' => $hotelId,
            'status' => 3,
        );
        if (is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
        }
        if (strpos($hotelId, ',') !== false) {
            $option['hotelentity_id'] = array('in', $hotelId);
        } else {
            $option['hotelentity_id'] = $hotelId;
        }


        if($mobile){
            $option['mobile'] = $mobile;
        }
        if (!is_null($areaCode)) {
            $option['areacode'] = $areaCode;
        }
        $field = 'id,sum(deposit) as deposit, sum(receipt) as receipt,areacode,mobile,count(*) as checkin';
        if(isset($offset) && $pageSize){
            $item = $this->field($field)->where($option)->limit($offset,$pageSize)->group('mobile,areacode')->select();
            $total = count($item);
            return array('item' => empty($item) ? array() : $item,'page' => $total);
        }else{
            $data = $this->field($field)->where($option)->group('mobile,areacode')->select();
            return empty($data) ? array() : $data;
        }
    }
    /**
     * 根据客栈查询订单
     * @modify trainyao 2016-01-05 创建
     * @param  integer   $hotelId   客栈id
     * @param  mixin   $brief       是否获取概要信息,默认是
     * @return array                订单信息,
     */
    public function getInfoByHotelAndTime($hotelId, $startDate = null, $endDate = null) {
        $option = array(
            'hotelentity_id' => $hotelId,
            'status' => 3,
        );
        if($startDate && $endDate){
            $option['updatetime'] =  array('between',[$startDate, $endDate]);
        }else if($startDate){
            $option['updatetime'] =  array('egt',$startDate);
        }
        $field = 'id,sum(deposit) as deposit, sum(receipt) as receipt,areacode,mobile,count(*) as checkin';
        $data = $this->where($option)->field($field)->group('mobile,areacode')->select();
        return empty($data) ? array() : $data;
    }

    /**
     * 根据客栈查询订单
     * @modify trainyao 2016-01-05 创建
     * @param  integer   $hotelId   客栈id
     * @param  mixin   $brief       是否获取概要信息,默认是
     * @return array                订单信息,
     */
    public function getInfoByHotelAndDate($hotelId, $startDate = null, $endDate = null, $status = null, $charity = false) {
        $option = array(
            'hotelentity_id' => $hotelId,
            'status' => array('in', '2,3'),
        );
        if (!is_null($status)) {
            $option['status'] = $status;
        }
        if ($charity === true) {
            $option['charityprice'] = array('neq', 0);
        }
        if($startDate && $endDate){
            $option['createtime'] =  array('between',[$startDate, $endDate]);
        }else if($startDate){
            $option['createtime'] =  array('egt',$startDate);
        }

        $data = $this->where($option)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 根据orderId查询订单
     * @modify wenhui 2016-01-05 创建
     */
    public function getInfoByOrderAndDate($orderId, $startDate = null, $endDate = null, $status = null, $charity = false) {
        $option = array(
            'id' => $orderId,
        );
        if (!is_null($status)) {
            $option['status'] = $status;
        }
        if ($charity === true) {
            $option['charityprice'] = array('neq', 0);
        }
        if($startDate && $endDate){
            $option['createtime'] =  array('between',[$startDate, $endDate]);
        }else if($startDate){
            $option['createtime'] =  array('egt',$startDate);
        }

        $data = $this->where($option)->select();

        return empty($data) ? array() : $data;
    }

     /**
     * 根据客栈查询订单
     * @modify trainyao 2016-01-05 创建
     * @param  integer   $hotelId   客栈id
     * @param  mixin   $brief       是否获取概要信息,默认是
     * @return array                订单信息,
     */
    public function getAllInfoByHotel($hotelId) {
        $data = $this->field('id')->where(array('hotelentity_id' => $hotelId))->select();
        return empty($data) ? array() : $data;
    }

    public function getOrderCheckout($hotelId, $mobile, $areaCode = null) {
        $option = array(
            'hotelentity_id' => $hotelId,
            'mobile' => $mobile,
            'status' => 3,
        );
        if (!is_null($areaCode)) {
            $option['areacode'] = $areaCode;
        }

        $result = $this->field('id')->where($option)->order('id desc')->find();
        return $result;
    }

    public function getOrderCheckoutAndCheckIn($hotelId, $mobile, $areaCode) {
        $option = array(
            'hotelentity_id' => $hotelId,
            'mobile' => $mobile,
            'areacode' => $areaCode,
            'status' => 3,
        );
        $field = 'sum(deposit) as deposit,sum(receipt) as receipt,count(*) as checkin';
        $data = $this->field($field)->where($option)->select();
        return empty($data) ? array() : $data;
    }
    /**
     * 添加父订单记录
     * @modify 2015-01-07 安哥
     * @modify wenhui 2017-05-04 增加区号
     * @param integer $hotelId         客栈id
     * @param type $channelId
     * @param type $deposit
     * @param type $payment
     * @param type $receipt
     * @param type $userId
     * @param type $mobile
     * @param type $name
     * @param type $identity
     * @param type $remark
     * @param string $areaCode
     * @return type  //$hotelChannelId, $deposit, $payment, $receipt, $orderUserId, $mobile, $identity, $remark
     */
    public function addInfo($hotelId, $channelId, $deposit, $payment, $receipt, $userId, $mobile, $name, $identity, $remark, $status, $orig, $discount, $discountrule, $areaCode) {
        $options = array(
            'hotelentity_id' => $hotelId,
            'hotelchannel_id' => $channelId,
            'deposit' => $deposit,
            'payment' => $payment,
            'receipt' => $receipt,
            'userlogin_id' => $userId,
            'mobile' => $mobile,
            'name' => $name,
            'identity' => $identity,
            'remark' => $remark,
            'status' => $status,
            'orig' => $orig,
            'discount' => $discount,
            'discountrule' => $discountrule,
            'areacode' => $areaCode,
        );
        return $this->add($options);
    }

    /**
     * 修改订单
     * @modify songanwei 2016-01-07
     * @modify trainyao 2016-01-16       修改为可以修改部分信息
     * @modify wenhui 2017-05-04 增加区号
     * @param integer $orderId  订单ID
     * @param integer $hotelChannelId  渠道ID
     * @param integer $deposit 押金
     * @param integer $payment 应付金额
     * @param integer $receipt 已收金额
     * @param integer $orderUserId 预订人用户ID
     * @param string $mobile 预订人手机号
     * @param string $name 预订人名称
     * @param string $identity 预订人身份
     * @param string $remark   备注
     * @param string $status
     * @param string $orig
     * @param string $areaCode   区号
     * @param string $charityPrice   义卖价
     */
    public function saveInfo($orderId, $hotelChannelId, $deposit = null, $payment = null, $receipt = null, $orderUserId = null, $mobile = null, $name, $identity = null, $remark = null, $status = null, $orig = null, $areaCode = null, $charityPrice = null) {
        $options = array();
        if (!empty($hotelChannelId)) {
            $options['hotelchannel_id'] = $hotelChannelId;
        }
        if (!is_null($deposit)) {
            $options['deposit'] = $deposit;
        }
        if (!is_null($payment)) {
            $options['payment'] = $payment;
        }
        if (!is_null($receipt)) {
            $options['receipt'] = $receipt;
        }
        if (!is_null($orderUserId)) {
            $options['userlogin_id'] = $orderUserId;
        }
        if (!is_null($mobile)) {
            $options['mobile'] = $mobile;
        }
        if (!empty($name)) {
            $options['name'] = $name;
        }
        if (!is_null($identity)) {
            $options['identity'] = $identity;
        }
        if (!is_null($remark)) {
            $options['remark'] = $remark;
        }
        if (!is_null($status)) {
            $options['status'] = $status;
        }
        if (!is_null($orig)) {
            $options['orig'] = $orig;
        }
        if (!is_null($areaCode)) {
            $options['areacode'] = $areaCode;
        }
        if (!is_null($charityPrice)) {
            $options['charityprice'] = $charityPrice;
        }

        $result = $this->where(array('id' => $orderId))->save($options);
        return $result === false ? false : true;
    }

}
